#include "shangjiajm.h"
#include "ui_shangjiajm.h"
#include "mainwindow.h"

extern QSqlDatabase db;
shangjiajm::shangjiajm(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::shangjiajm)
{
    ui->setupUi(this);
    //设置表格的列数
    ui->tableWidget->setColumnCount(3);
    //设置列宽
    ui->tableWidget->setColumnWidth(0,168);
    ui->tableWidget->setColumnWidth(1,168);
    ui->tableWidget->setColumnWidth(1,168);
    //设置表格字段名
    ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"订单："<<"商品："<<"商品数量");
    //设置表格的行数
    ui->tableWidget->setRowCount(20);
}

shangjiajm::~shangjiajm()
{
    delete ui;
}

void shangjiajm::on_pushButton_clicked()
{
    int j = 0;
    int j1 = 0;
    QString sql = QString("SELECT * FROM llbcai WHERE price = %1").arg(1.1);

    QSqlQuery query(db);
    bool ret = query.exec(sql);

    ui->tableWidget->clear();//把表格列表框清空
    ui->tableWidget->setHorizontalHeaderLabels(QStringList()<<"订单号："<<"商品："<<"商品数量：");

    if (!ret) {
        qDebug() << "查询失败:" ;
    } else {
        while (query.next()) {
            for(int i = 0;i < 3;i++){


                QString str = query.value(i).toString();
                qDebug()<<str;
                QTableWidgetItem * item = new QTableWidgetItem(str);
                ui->tableWidget->setItem(j,i,item);


            }
            j ++;
        }
    }
    QString num;
    num = ui->lineEdit->text();
    bool ok;
    int number = num.toInt(&ok); // 使用默认的十进制转换

    QString sql1 = QString("DELETE FROM llbcai WHERE id = %1").arg(number);

    QSqlQuery query1(db);
    bool ret1 = query1.exec(sql1);
    if (!ret1) {
        qDebug() << "查询失败:" ;
    } else {
        while (query.next()) {
            for(int i = 0;i < 3;i++){


                QString str = query.value(i).toString();
                qDebug()<<str;
                QTableWidgetItem * item = new QTableWidgetItem(str);
                ui->tableWidget->setItem(j1,i,item);


            }
            j1 ++;
        }
    }

}

